train_data = [
    0.4003; 0.3988; 0.3998; 0.3997;
    0.2554; 0.3139; 0.2627; 0.3802;
    0.5632; 0.7687; 0.0524; 0.7586
];

train_labels = [1; 1; 1; 1; 2; 2; 2; 2; 3; 3; 3; 3];

test_data = [
    0.4010; 0.3995; 0.3991;
    0.3287; 0.3160; 0.2924;
    0.4243; 0.5005; 0.6769
];

k = 1;
knn_model = fitcknn(train_data, train_labels, 'NumNeighbors', k);
% 使用训练好的模型进行预测
predictions = predict(knn_model, test_data);

% 打印结果
disp(predictions);

predictions = zeros(size(test_data, 1), 1); % 用于存储预测结果


for i = 1:size(test_data, 1)
    min_distance = inf;
    min_index = 0;
    
    for j = 1:size(train_data, 1)
        distance = norm(test_data(i, :) - train_data(j, :)); % calculate the distance
        
        if distance < min_distance
            min_distance = distance;
            min_index = j;
        end
    end
    
    predictions(i) = train_labels(min_index);
end

disp(predictions);